package run.bottle.swagger.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;

@Configuration
public class SwaggerConfig {

    /**
     * 配置Docket Bean实例
     * @return
     */
    @Bean
    public Docket docket(Environment environment) {

        //设置要显示的Swagger环境
        Profiles profiles = Profiles.of("dev","test");

        //通过environment.acceptsProfiles()判断是否处于自己设定的环境中
        boolean flag = environment.acceptsProfiles(profiles);

        /**
         * select()
         *
         *   apis() 配置扫描接口的方式
         *      basePackage 指定要扫描的包
         *      any() 扫描全部
         *      none() 不扫描
         *      withClassAnnotation 扫描类上的注解
         *      withMethodAnnotation 扫描方法上的注解
         *
         *   paths() 过滤路径
         *
         */
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(flag)//是否启动swa
                .groupName("正式版")// gger
                .select()
                .apis(RequestHandlerSelectors.basePackage("run.bottle.swagger.controller"))
                //.paths(PathSelectors.ant("/app/**"))
                .build();
    }

    /**
     * 配置分组A
     */
    @Bean
    public Docket docket1() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("A");
    }

    /**
     * 配置分组B
     */
    @Bean
    public Docket docket2() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("B");
    }

    /**
     * 配置分组C
     */
    @Bean
    public Docket docket3() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("C");
    }

    //配置Swagger信息
    private ApiInfo apiInfo() {

        // 作者信息
        Contact contact = new Contact("bottle", "http://www.bottle.run", "38707145@qq.com");

        return new ApiInfo("Bottle Swagger Api文档",
                "努力奔向更美好的你",
                "v1.0",
                "http://www.bottle.run",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }
}
